package com.wehealth.electrocardiograph_two_client.presenter

import android.annotation.SuppressLint
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import com.wehealth.electrocardiograph_two_client.activity.HTMLActivity
import com.wehealth.electrocardiograph_two_client.databinding.ActivityHtmlBinding

class HtmlPresenter(val activity: HTMLActivity, val dataBinding: ActivityHtmlBinding) {

    @SuppressLint("SetJavaScriptEnabled")
    fun loadContent(title: String, url: String) {
        dataBinding.titleName.text = title
        url?.let { dataBinding.htmlWebview.loadUrl(it) }
        dataBinding.htmlWebview.webChromeClient = WebChromeClient()
        dataBinding.htmlWebview.webViewClient = WebViewClient()
        dataBinding.htmlWebview.settings.javaScriptEnabled = true
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        dataBinding.htmlWebview.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url)
                return true
            }
        }
    }


    fun finishPage() {
        activity.finish()
    }
}